<?php
class ad extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览广告', '1-2');
		$da = $this -> pub -> get_setting('广告', 'Advertising');
		$this -> load -> view('adminx/ad', $da);
	}

	function add() {
		$this -> pub -> logp('创建广告', '1-2');
		$da['name'] = $this -> input -> post('name');
		$da['url'] = $this -> input -> post('url');
		$da['state'] = $this -> input -> post('state');
		$da['pic'] = $this -> input -> post('pic');
		$da['position'] = $this -> input -> post('position');
		$this -> db -> insert('plug_ad', $da);
		echo json_encode(array('type' => 1));
	}

	function up() {
		$this -> pub -> logp('广告修改 - ' . $this -> input -> post('idx'), '1-2');
		$da['name'] = $this -> input -> post('name');
		$da['url'] = $this -> input -> post('url');
		$da['state'] = $this -> input -> post('state');
		if($this -> input -> post('pic')){
			$da['pic'] = $this -> input -> post('pic');
		}
		$da['position'] = $this -> input -> post('position');
		$this -> db -> update('plug_ad', $da, array('id' => $this -> input -> post('idx')));
		echo json_encode(array('type' => 1));
	}

	function get_one() {
		$this -> pub -> logp('获取广告信息 - ' . $this -> input -> post('idx'), '1-2');
		$q = $this -> db -> get_where('plug_ad', array('id' => $this -> input -> post('idx')));
		if ($q -> num_rows() > 0) {
			$da = $q -> row_array(0);
			$da['typex'] = 1;
			echo json_encode($da);
		} else {
			echo json_encode(array('typex' => 2));
		}
	}

	function del() {
		$this -> pub -> logp('广告关闭 - ' . $this -> input -> post('idx'), '1-2');
		$da['type'] = 1;
		$this -> db -> update('plug_ad', array('state' => 2), array('id' => $this -> input -> post('idx')));
		echo json_encode($da);
	}

	function get() {
		$this -> pub -> logp('获取广告信息', '1-2');
		if ($this -> input -> post('key')) {
			$this -> db -> like('ad', $this -> input -> post('key'));
		}
		$q = $this -> db -> get_where('plug_ad');
		$state[1] = '启用';
		$state[2] = '关闭';
		$position[1] = '首页顶部';
		$position[2] = '首页底部';
		$position[3] = '文章顶部';
		$position[4] = '文章底部';
		$da['content'] = '';

		$ls = $q -> result_array();
		foreach ($ls as $v) {
			$da['content'] .= '<tr>';
			$da['content'] .= '<td>' . $v['name'] . '</td>';
			$da['content'] .= '<td>' . $v['url'] . '</td>';
			$da['content'] .= '<td><img width="260" src="' . $this -> pub -> oss_dz($v['pic']) . '" /></td>';
			$da['content'] .= '<td>' . $position[$v['position']] . '</td>';
			$da['content'] .= '<td>' . $state[$v['state']] . '</td>';
			$da['content'] .= '<td><button onclick="show_up(\'' . $v['id'] . '\')" type="button"  class="btn default"><i class="fa fa-edit"></i>修改</button>  <button onclick="del(\'' . $v['id'] . '\',\'' . $v['ad'] . '\')" type="button" class="btn red"><i class="fa fa-trash-o"></i> 关闭</button></td>';
			$da['content'] .= '</tr>';
		}
		$da['type'] = 1;
		echo json_encode($da);
	}

}
?>